package cn.itcast.cd.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.BeanUtils;

import cn.itcast.cd.dao.IBaseDao;
import cn.itcast.cd.dao.IPurchaseBillDao;
import cn.itcast.cd.domain.PurchaseBill;
import cn.itcast.cd.domain.PurchaseBillItem;
import cn.itcast.cd.query.IQuery;
import cn.itcast.cd.resultPage.ResultPage;
import cn.itcast.cd.service.IPurchaseBillService;
import cn.itcast.cd.utils.MD5;
import cn.itcast.cd.utils.SessionUtils;

public class PurchaseBillServiceImpl implements IPurchaseBillService {

	private IPurchaseBillDao PurchaseBillDao;

	public void setPurchaseBillDao(IPurchaseBillDao PurchaseBillDao) {
		this.PurchaseBillDao = PurchaseBillDao;
	}

	@Override
	public void save(PurchaseBill PurchaseBill) {

		PurchaseBill.setBuyer(SessionUtils.getUserInSession());
		PurchaseBill.setInputUser(SessionUtils.getUserInSession());
		PurchaseBill.setInputTime(new Date());
		PurchaseBill.setVdate(new Date());
		
		for (PurchaseBillItem item : PurchaseBill.getItems()) {
			item.setAmount(item.getNum().multiply(item.getPrice()));
			PurchaseBill.setAmount(PurchaseBill.getAmount().add(item.getAmount()));
		}

		// 这一步就是把BILL添加到BILLITEMS里面.这样,BILLITEMS才会有BILL_ID;
		for (PurchaseBillItem items : PurchaseBill.getItems()) {
			items.setBill(PurchaseBill);
		}
		PurchaseBillDao.save(PurchaseBill);
	}

	@Override
	public PurchaseBill get(Long id) {
		return (PurchaseBill) PurchaseBillDao.get(id);
	}

	@Override
	public ResultPage query(String hql, Object[] params, int start, int limit) {
		return PurchaseBillDao.query(hql, params, start, limit);
	}

	@Override
	public void delete(Long id) {
		PurchaseBillDao.delete(id);
	}

	@Override
	public void update(PurchaseBill purchaseBill) {
		
		//因为现在更新传上来的数据,有可能将之前的BILL中的某些的ITEM删除了.但是更新后,BILL是变了,但删除的ITEM还存在于数据库.所以
		//我们要将新传上来的BILL和数据库中的BILL中的ITEM进行比较,将不存在 的ITEM删除.
		PurchaseBill DBBill = PurchaseBillDao.get(purchaseBill.getId());//得到DB原来的BILL
		//PurchaseBillDao.update(purchaseBill);//然后将新 的BILL保存掉.再循环老的将不存在的从DB中干掉.如果先不保存,那么你修改新添的ITEM也会被干掉.
		
		for (Iterator<PurchaseBillItem> iterator = DBBill.getItems().iterator();iterator.hasNext();) {
			PurchaseBillItem oldItem = iterator.next();
			
			if(!(purchaseBill.getItems().contains(oldItem))){
				iterator.remove();
			}
		}
		System.out.println("--------------------oldItems----------------"+DBBill.getItems().size());

		purchaseBill.setBuyer(SessionUtils.getUserInSession());
		purchaseBill.setInputUser(SessionUtils.getUserInSession());
		purchaseBill.setInputTime(new Date());
		purchaseBill.setVdate(new Date());
		
		
		for (PurchaseBillItem item : purchaseBill.getItems()) {
			item.setAmount(item.getNum().multiply(item.getPrice()));
			purchaseBill.setAmount(purchaseBill.getAmount().add(item.getAmount()));
		}
		
		

		// 这一步就是把BILL添加到BILLITEMS里面.这样,BILLITEMS才会有BILL_ID;
		for (PurchaseBillItem items : purchaseBill.getItems()) {
			items.setBill(DBBill);
		}
		//PurchaseBillDao.save(PurchaseBill);
		PurchaseBillDao.update(DBBill);
	}

	@Override
	public ResultPage queryEntity(IQuery query) {

		return PurchaseBillDao.queryEntity(query);
	}

	@Override
	public List<PurchaseBill> queryEntity(String conditioins, Object[] params) {
		
		return PurchaseBillDao.queryEntity(conditioins, params);
	}

}
